package l1j.opqlo.Timer.Controler;

import java.util.Collection;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.WorldElf;
import l1j.server.Thread.PcOtherThreadPool;
import l1j.server.server.model.Instance.L1PcInstance;

/**
 * PC 可見物更新處理 時間軸(精靈)
 *
 * @author dexc
 *
 */
public class UpdateObjectETimer extends TimerTask {

	private static final Logger _log = Logger
			.getLogger(UpdateObjectETimer.class.getName());

	private ScheduledFuture<?> _timer;

	@Override
	public void run() {
		try {
			final Collection<L1PcInstance> allPc = WorldElf.get().all();
			// 不包含元素
			if (allPc.isEmpty()) {
				return;
			}

			for (final L1PcInstance tgpc : allPc) {
				if (UpdateObjectCheck.check(tgpc)) {
					tgpc.updateObject();
				}
			}

			/*
			 * for (final L1PcInstance iter : allPc) { if
			 * (UpdateObjectCheck.check(iter)) { iter.updateObject(); } }
			 */

		} catch (final Exception e) {
			_log.log(Level.SEVERE, "Pc 可見物更新處理時間軸(精靈)異常重啟", e);
			PcOtherThreadPool.get().cancel(this._timer, false);
			final UpdateObjectETimer objectETimer = new UpdateObjectETimer();
			objectETimer.start();
		}
	}

	public void start() {
		final int timeMillis = 300;// 0.35秒
		this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this,
				timeMillis, timeMillis);
	}
}
